<template>
  <div class="app-container">
    <avue-crud
      :option="option"
      :data="data"
      :table-loading="loading"
      @on-load="onLoad"
      @search-change="searchChange"
      @search-reset="resetChange"
      :span-method="spanMethod"
    >
    </avue-crud>
  </div>
</template>

<script>
import { searchDataFilling, getbank } from '@/api/report.js'
export default {
  data() {
    return {
      data: [],
      loading: false,
      searchList: {},
      jgOptions: [],
      option: {
        headerAlign: 'center',
        align: 'center',
        border: true,
        index: true,
        indexLabel: '序号',
        indexWidth: 100,
        card: true,
        defaultExpandAll: true,
        dialogDrag: false,
        labelWidth: 135,
        viewBtn: false,
        delBtn: false,
        editBtn: false,
        addBtn: false,
        excelBtn: true,
        selection: false,
        searchLabelWidth: '35%',
        searchSpan: 6,
        searchMenuSpan: 4,
        columnBtn: true,
        refreshBtn: false,
        selectClearBtn: false,
        // searchBtn: true,
        menu: false,
        searchShowBtn: true,

        column: [
          {
            label: '银行机构名称',
            prop: 'yhjgmc',
            display: false,
          },
          {
            label: '上报数据年月',
            prop: 'sbsjny',
            display: false,
            search: true,
            type: 'month',
            format: 'yyyy-MM',
            valueFormat: 'yyyy-MM',
            pickerOptions: {
              disabledDate(time) {
                const t = new Date().getDate()
                return time.getTime() > Date.now()
              },
            },
          },
          {
            label: '本年累计提出融资申请的企业户数（户）',
            prop: 'bnljRzsqQyhs',
            display: false,
          },
          {
            label: '获得融资的企业户数（户）',
            prop: 'rzhs',
            display: false,
          },
          {
            label: '其中：信用贷款户数',
            prop: 'xyDkhs',
            display: false,
          },
          {
            label: '获得融资金额（万元）',
            prop: 'rzje',
            display: false,
          },
          {
            label: '其中：信用贷款金额（万元）',
            prop: 'xyDkje',
            display: false,
          },
          {
            label: '获得融资的企业平均贷款利率（%）',
            prop: 'jzlv',
            display: false,
          },
          {
            label: '本年累计首次申请授信的企业户数（户）',
            prop: 'bnljSxQyhs',
            searchSpan: 6,
            display: false,
          },
          {
            label: '首次申请并获得授信的企业户数（户）',
            prop: 'scHdsxQyhs',
            display: false,
            searchSpan: 6,
          },
          {
            label: '首次申请并获得授信的贷款金额（万元）',
            prop: 'scHdsxDkje',
            display: false,
          },
          {
            label: '首次申请并获得融资的企业户数（户）',
            prop: 'scSqhdrzQyhs',
            display: false,
          },
          {
            label: '首次申请并获得融资金额（万元）',
            prop: 'scSqhdrzJe',
            display: false,
          },
        ],
      },
    }
  },
  created() {
    this.getJgOptions()
  },
  methods: {
    getJgOptions() {
      getbank().then((res) => {
        this.jgOptions = res.data.map((item) => item.deptValue)
      })
    },
    onLoad() {
      let searchList = this.searchList
      let params = {
        date: searchList.sbsjny ? searchList.sbsjny : this.getTime(),
      }
      this.loading = true
      searchDataFilling(params).then((data) => {
        if (data.status) {
          this.loading = false
          if (data.data.length > 0) {
            let list = data.data
            let last = list[list.length - 1]
            list.pop()
            this.jgOptions.forEach((item) => {
              //判断list是否有当前机构
              let index = list.findIndex((ite) => ite.yhjgmc == item)
              //否，将当前机构添加到list
              if (index == -1) {
                list.push({
                  yhjgmc: item,
                  sbsjny: params.date,
                  bnljRzsqQyhs: '-',
                  rzhs: '-',
                  xyDkhs: '-',
                  rzje: '-',
                  xyDkje: '-',
                  jzlv: '-',
                  bnljSxQyhs: '-',
                  scHdsxQyhs: '-',
                  scHdsxDkje: '-',
                  scSqhdrzQyhs: '-',
                  scSqhdrzJe: '-',
                })
              }
            })
            list.push(last)
            this.data = list
          } else {
            this.data = []
          }
        }
      })
    },
    getTime() {
      let curDate = new Date()
      let year = curDate.getFullYear()
      let month =
        curDate.getMonth() + 1 > 9
          ? curDate.getMonth() + 1
          : '0' + (curDate.getMonth() + 1)
      let day = curDate.getDate()
      //2022-11、2022-12比较特殊（获取的都是2022-11的数据）
      if (
        year == 2022 &&
        (curDate.getMonth() + 1 == 11 || curDate.getMonth() + 1 == 12)
      ) {
        return '2022-11'
      } else {
        if (day > 10) {
          //day > 10 获取当月填报报表数据(即累计到上月的数据)
          return curDate.getMonth() > 0
            ? year +
                '-' +
                (curDate.getMonth() > 9
                  ? curDate.getMonth()
                  : '0' + curDate.getMonth())
            : year - 1 + '-12'
        } else {
          //获取上月填报报表数据(即累计到上上月的数据)
          if (curDate.getMonth() - 1 > 0) {
            //3月以上 获取(m-2)的数据,m代表当前月份
            month =
              curDate.getMonth() - 1 > 9
                ? curDate.getMonth() - 1
                : '0' + (curDate.getMonth() - 1)
            return year + '-' + month
          } else if (curDate.getMonth() - 1 == 0) {
            //2月 获取去年12月的数据
            return year - 1 + '-12'
          } else {
            //1月 获取去年11月的数据
            return year - 1 + '-11'
          }
        }
      }
    },
    /* 清空 */
    resetChange() {
      this.searchList = {}
      this.onLoad()
    },
    /* 搜索筛选 */
    searchChange(params, done) {
      done()
      this.searchList = {
        ...params,
      }
      this.onLoad()
    },
    spanMethod({ row, column, rowIndex, columnIndex }) {
      let lastRow = this.data.length
      if (lastRow > 0 && rowIndex == lastRow - 1) {
        if (columnIndex == 1) {
          return [1, 3]
        } else if (columnIndex == 0 || columnIndex == 2) {
          return [0, 0]
        }
      }
    },
    /* rowStyle({row, rowIndex}){
                let lastRow = this.data.length;
                if(rowIndex == lastRow-1){
                    return {
                        'color':'#fff',
                        'background':'#559beb'
                        }
                }
            } */
  },
}
</script>

<style scoped="scoped" lang="scss">
/deep/ .avue-crud__pagination {
  text-align: center;
}
// /deep/ .avue-crud .el-table th{
//     background: #559beb;
//     color: #fff;
// }
</style>
